package com.seveneight.baijiesteward.ui.main.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.adapter.SelectBankApplicationAdapter;
import com.seveneight.baijiesteward.base.BaseActivity;
import com.seveneight.baijiesteward.bean.AnyBean;
import com.seveneight.baijiesteward.bean.BankCardListBean;
import com.seveneight.baijiesteward.http.MyCallBack;
import com.seveneight.baijiesteward.ui.user.model.CradBankListModel;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 卡通道选择
 */
public class SelectApplicationBankCardActivity extends BaseActivity implements View.OnClickListener,AdapterView.OnItemClickListener{

    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.bank_listview)
    ListView listView;
    List<BankCardListBean> listData=new ArrayList<>();
    SelectBankApplicationAdapter mAdapter;
    CradBankListModel model;
    String[] bankArray;
    String[] themeArray;
    String[] tableArray;

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_select_application_bank_card);
    }

    @Override
    protected void initView() {
        bankArray=getIntent().getStringArrayExtra("bank");
        themeArray=getIntent().getStringArrayExtra("theme");
        tableArray=getIntent().getStringArrayExtra("table");

    }

    @Override
    protected void initData() {
        model=new CradBankListModel();
        tv_title.setText("选择卡面");
        mAdapter=new SelectBankApplicationAdapter(this,listData);
        listView.setAdapter(mAdapter);
        model=new CradBankListModel();
        Map<String,Object> params=new HashMap<>();
        JSONArray jsonArray = new JSONArray();
        for (int i=0;i<bankArray.length;i++){
            jsonArray.put(bankArray[i]);
        }
        params.put("BankIds",jsonArray);

        jsonArray = new JSONArray();
        for (int i=0;i<themeArray.length;i++){
            jsonArray.put(themeArray[i]);
        }
        params.put("TopicIds",jsonArray);

        jsonArray = new JSONArray();
        for (int i=0;i<tableArray.length;i++){
            jsonArray.put(tableArray[i]);
        }
        params.put("TagIds",jsonArray);
        model.getBankList(this, jsonFormat.AddCardBank(params), new MyCallBack<AnyBean>() {
            @Override
            public void onSuccess(AnyBean anyBean) {
                listData= JSON.parseArray(anyBean.getBankCardList(),BankCardListBean.class);
                mAdapter.refresh(listData);
            }

            @Override
            public void onFailed() {

            }
        });
    }

    @Override
    protected void setListener() {
//        listView.setOnItemClickListener(this);

    }

    @OnClick({R.id.iv_back})
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK){
            switch (requestCode){
                case 22:
                    finish();
                    break;
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//        Intent intent=new Intent(this,BankCardInfoActivity.class);
//        intent.putExtra("StationChannelId",listData.get(i).getStationChannelId());
//        intent.putExtra("StationBankCardChannelId",listData.get(i).getStationBankCardChannelId());
//        startActivityIfNeeded(intent,22);
    }
}
